#!/bin/bash
#安装node.js
if [ ! "$(command -v node)" ]; then
    echo "node 没有安装,正在重新安装"
    apt  update
    apt install curl -y
    curl -sL https://deb.nodesource.com/setup_19.x |  bash -
    apt update
    apt -y install nodejs
fi
#云崽安装
function Yunzai_install(){
    echo "Yunzai-Bot-install"
    if [ ! -d "/root/Yunzai-Bot" ]; then 
        git clone --depth=1 -b main https://gitee.com/Le-niao/Yunzai-Bot.git /root/Yunzai-Bot
        if [ ! -d "/root/Yunzai-Bot" ]; then
            echo "error:Yunzai-bot Download failed"
            exit 0
        else
            echo "success,Replicating directory"
        fi
    fi
    cd /root/Yunzai-Bot/
    echo "Start installing depandencies "
    if [ ! -d "node-mudules/" ]; then
        if ! type pnpm >/dev/null; then
            npm install pnpm -g
        fi;
        if ! type cnpm >/dev/null; then
            npm install cnpm -g --registry=https://registry.npmmirror.com
        fi;
        pnpm install -P
        echo "Installed successfully"
    else
        echo "Dependencies have been installed"
    fi
}

#安装插件
function plugins_install(){
    cd /root/Yunzai-Bot/
    echo "Start instaling install Guoba-Plugin (id:01)"
    if [ ! -d plugins/Guoba-Plugin/ ]; then
        git clone --depth=1 https://gitee.com/guoba-yunzai/guoba-plugin.git ./plugins/Guoba-Plugin/
        pnpm install --filter=guoba-plugin
        if [ ! -d plugins/Guoba-Plugin/ ]; then
            echo "error:Guoba-Plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi
    
    echo "Start instaling install miao-plugin (id:02)"
    if [ ! -d plugins/miao-plugin/ ]; then
        git clone https://gitee.com/yoimiya-kokomi/miao-plugin.git ./plugins/miao-plugin/
        pnpm add image-size -w
        if [ ! -d plugins/miao-plugin/ ]; then
            echo "error:miao-plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi
    
    echo "Start instaling install xiaoyao-cvs-plugin (id:03)"
    if [ ! -d plugins/xiaoyao-cvs-plugin/ ]; then
        git clone https://gitee.com/Ctrlcvs/xiaoyao-cvs-plugin.git  ./plugins/xiaoyao-cvs-plugin/
        if [ ! -d plugins/xiaoyao-cvs-plugin/ ]; then
            echo "error:xiaoyao-cvs-plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi

    echo "Start instaling install xianxin-plugin (id:04)"
    if [ ! -d plugins/xianxin-plugin/ ]; then
        git clone https://gitee.com/xianxincoder/xianxin-plugin.git ./plugins/xianxin-plugin/ 
        if [ ! -d plugins/xianxin-plugin/ ]; then
            echo "error:xianxin-plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi
    echo "Start instaling install earth-k-plugin (id:05)"
    if [ ! -d plugins/earth-k-plugin/  ]; then
        git clone https://gitee.com/SmallK111407/earth-k-plugin.git ./plugins/earth-k-plugin/ 
        pnpm add https-proxy-agent -w
        if [ ! -d plugins/earth-k-plugin/  ]; then
            echo "error:earth-k-plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi

    echo "Start instaling install lin-plugin (id:06)"
    if [ ! -d plugins/lin-plugin/  ]; then
        git clone https://gitee.com/go-farther-and-farther/lin-plugin.git ./plugins/lin-plugin/ 
        if [ ! -d plugins/lin-plugin/ ]; then
            echo "error:lin-plugin  Download failed"
            exit
        fi
        echo "Installed successfully"
    fi

    echo "Start instaling install xitian-plugin (id:07)"
    if [ ! -d plugins/xitian-plugin/ ]; then
        git clone https://github.com/XiTianGame/xitian-plugin.git ./plugins/xitian-plugin/
        if [ ! -d plugins/xitian-plugin/  ]; then
            echo "error: xitian-plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi


    echo "Start instaling install liulian-plugin (id:8)"
    if [ ! -d plugins/liulian-plugin/ ]; then
    git clone https://gitee.com/huifeidemangguomao/liulian-plugin.git ./plugins/liulian-plugin/
        if [ ! -d plugins/liulian-plugin/  ]; then
            echo "error: liulian-plugin Download failed"
            exit
        fi
        echo "Installed successfully"
    fi
}


function main_menu() {
    clear
    echo -e "选择需要执行的功能："
    echo -e "\n 1.仅安装云崽（不包含插件） \n 2.安装云崽和常用插件   \n 0.退出不安装 \n"
    read -p "请输入你的选择（输入数字）:" run_function
    if [[ "${run_function}" == "1" ]]; then
        Yunzai_install
        bash <(curl -sL https://gitee.com/Liplay-1/yun-zai-box/raw/master/conf.sh) #配置脚本
    elif [[ "${run_function}" == "2" ]]; then
        Yunzai_install
        plugins_install
        bash <(curl -sL https://gitee.com/Liplay-1/yun-zai-box/raw/master/conf.sh) #配置脚本
    elif [[ "${run_function}" == "0" ]]; then
            start=0
    else
        main_menu
    fi
}
main_menu


